<?php

function beranda_get_statuses($before,$after) {
	$where = '';
	$sort = 'DESC';
	if ($before != -1) {
		$sort = 'DESC';
		$where = ' WHERE stat_id < '.$before.' ';
	} elseif ($after != -1) {
		$sort = 'ASC';
		$where = ' WHERE stat_id > '.$after.' ';
	}
	$statuses = db_array('SELECT stat_id,stat_date,stat_txt,reply_stat,restat_from,user_id,username,name FROM status INNER JOIN users ON stat_user = user_id '.$where.' ORDER BY stat_id '.$sort.' LIMIT 0,11'); // equal 11 okay to more
	if ($sort == 'ASC') {
		$statuses = array_reverse($statuses);
	}
	return extend_statuses($statuses);
}

if (defined('NO_DIRECT')) {
	if (isset($_GET['before'])) {
		$statuses = beranda_get_statuses(intval($_GET['before']),-1);
	} elseif (isset($_GET['after'])) {
		$statuses = beranda_get_statuses(-1,intval($_GET['after']));
	} else {
		$statuses = beranda_get_statuses(-1,-1);
	}
	if (isset($_GET['ajax'])) {
		header('Content-type: text/plain');
		$json['count'] = count($statuses);
		$json['statuses'] = $statuses;
		echo json_encode($statuses);
		echo PHP_EOL.json_encode($json);
	} else {
		$post = get_posts($session['user']['user_id']);
		$following = get_followings($session['user']['user_id']);
		$follower = get_followers($session['user']['user_id']);
		?>
		<p>
		@<?php echo $session['user']['username']; ?> - <?php echo $session['user']['name']; ?><br />
		<?php echo $post['count']; ?> post<br />
		<?php echo $following['count']; ?> following<br />
		<?php echo $follower['count']; ?> followers
		</p>
		<p>
		Informasi apa yang anda punya ?<br />
		<form action='<?php echo make_url('post-action',''); ?>' method='post'>
			<input type='text' name='status' />
			<button>Post</button>
		</form>
		</p>
		<?php
		foreach ($statuses as $status) {
			echo '<p><strong>@'.$status['username'].'</strong><br />'.show_status($status['stat_txt']).'<br />'.$status['stat_date'].'</p>'.PHP_EOL;
		}
	}
}
